-- This function is run last and is a good place to configuring
-- augroups/autocommands and custom filetypes also this just pure lua so
-- anything that doesn't fit in the normal config locations above can go here
local utils = require "astronvim.utils"
local is_available = utils.is_available
return function()
  -- Set up custom filetypes
  -- vim.filetype.add {
  --   extension = {
  --     foo = "fooscript",
  --   },
  --   filename = {
  --     ["Foofile"] = "fooscript",
  --   },
  --   pattern = {
  --     ["~/%.config/foo/.*"] = "fooscript",
  --   },
  -- }
  require "user.utils.autocmds"
  require "user.utils.keymap"
  -- local utils = require "astronvim.utils"
  -- local is_available = utils.is_available
  -- local lsp_mappings = require("astronvim.utils").empty_map_table()

  local opt = { noremap = true, silent = true }
  -- vim.keymap.set({ "n", "i", "v" }, "<Left>", "<Nop>", opt)
  -- vim.keymap.set({ "n", "i", "v" }, "<Right>", "<Nop>", opt)
  -- vim.keymap.set({ "n", "i", "v" }, "<Up>", "<Nop>", opt)
  -- vim.keymap.set({ "n", "i", "v" }, "<Down>", "<Nop>", opt)
  -- vim.keymap.set({ "n", "i", "v" }, "<ESC>", "<Nop>")
  -- vim.keymap.set({ "n", "i", "v" }, "<BS>", "<Nop>", opt)

  -- vim.keymap.set({ "n", "v" }, "n", "nzz", opt)
  -- vim.keymap.set({ "n", "v" }, "N", "Nzz", opt)
  -- vim.keymap.set({ "n", "v" }, "<leader>y", '"+y', opt)
  -- vim.keymap.set("n", "p", '"0p', opt)
  -- vim.keymap.set("n", "P", '"0P', opt)
  vim.cmd [[cnoremap <C-A> <Home>]]
  vim.cmd [[cnoremap <C-F> <Right>]]
  vim.cmd [[cnoremap <C-O> <C-F>]]
  vim.cmd [[cnoremap <C-B> <Left>]]

  vim.cmd [[inoremap <C-A> <Home>]]
  vim.cmd [[inoremap <C-E> <End>]]
  vim.cmd [[inoremap <C-F> <Right>]]
  vim.cmd [[inoremap <C-B> <Left>]]

  if vim.fn.executable "joshuto" == 1 then
    vim.keymap.set(
      { "n", "v" },
      "<leader>tj",
      require("user.utils.utils").toggle_joshuto(),
      { noremap = true, silent = true, desc = "ToggleTerm Joshuto" }
    )
  end

  if is_available "markdown-preview.nvim" then
    vim.keymap.set(
      { "n", "v" },
      "<leader>ze",
      "<cmd>MarkdownPreviewToggle<CR>",
      { noremap = true, silent = true, desc = "Open Markdown preview" }
    )
  end

  if is_available "vim-dadbod-ui" then
    vim.keymap.set(
      { "n", "v" },
      "<leader>td",
      "<cmd>DBUIToggle<CR>",
      { noremap = true, silent = true, desc = "ToggleTerm DBUI" }
    )
  end

  -- vim.keymap.set({ "n", "i", "v", "t" }, "<C-`>", "<cmd>ToggleTerm size=10 direction=horizontal<cr>")
  -- vim.keymap.set({ "n", "v" }, "<leader>ts", "<cmd>ToggleTermSendCurrentLine<cr>", { desc = "Send Line to Terminal" })
  -- vim.keymap.set({ "n", "v", "t" }, "<leader>j", "<cmd>ToggleTerm size=10 direction=horizontal<cr>",
  --   { noremap = true, silent = true, desc = "Toggle Below Terminal" }
  -- )
  -- vim.api.nvim_del_keymap("n", "<leader>lD")
  --
  -- vim.keymap.set("n", "<leader>lD",
  --   function()
  --     require("telescope.builtin").diagnostics({
  --       severity_limit = vim.diagnostic.severity.INFO })
  --   end, { desc = "Search diagnostics" })
  -- lsp_mappings.n["<leader>lD"] =
  -- {
  --   function()
  --     require("telescope.builtin").diagnostics({
  --       severity_limit = vim.diagnostic.severity.info })
  --   end,
  --   desc = "Search diagnostics"
  -- }

  -- vim.keymap.set("n", "<leader>rc", ":RunCode<CR>", opt)
  -- vim.keymap.set("n", "<leader>rf", ":RunFile<CR>", opt)
  -- vim.keymap.set("n", "<leader>rft", ":RunFile tab<CR>", opt)
  -- vim.keymap.set("n", "<leader>rp", ":RunProject<CR>", opt)
  -- vim.keymap.set("n", "q", ":RunClose<CR>", opt)
  -- vim.keymap.set("v", "J", ":m '>+1<CR>gv=gv", opt)
  -- vim.keymap.set("v", "K", ":m '<-2<CR>gv=gv", opt)
  -- vim.keymap.set({ "n", "v" }, "<leader>lz", "<cmd>Lazy<cr>", opt)
  -- vim.keymap.set({ "n", "v" }, "ga", ":EasyAlign")
  -- vim.keymap.set({ "n", "v" }, "ga", ":EasyAlign <CR><C-x>")
  -- vim.keymap.set({ "n", "v" }, "ga#", ":EasyAlign *#<CR>")
  -- vim.keymap.set("n", "gt", "[b", { noremap = true, silent = false })
  -- vim.keymap.set('n', '<leader>crf', ':CRFiletype<CR>', { noremap = true, silent = false })
  -- vim.keymap.set('n', '<leader>crp', ':CRProjects<CR>', { noremap = true, silent = false })
  -- vim.keymap.set("v", "<leader>a#", ":2,$Tabularize /#/l2<CR>", { noremap = true, silent = false })
  -- vim.keymap.set("v", "<leader>a=", ":Tabularize /=<CR>", { noremap = true, silent = false })
  -- vim.api.nvim_set_keymap("i", "<C-a>", "<Home>", opt)
  -- vim.api.nvim_set_keymap("i", "<C-e>", "<End>", opt)
  -- vim.api.nvim_set_keymap("i", "<C-f>", "<Right>", opt)
  -- vim.api.nvim_set_keymap("i", "<C-b>", "<Left>", opt)
  vim.api.nvim_set_keymap("n", "gd", "gdzz", opt)
  -- vim.api.nvim_set_keymap("n", "<leader>?", ":help <C-r><C-w><CR>", opt)
  -- vim.api.nvim_set_keymap("n", "<leader>?", "<cmd>help <C-r><C-w><CR>", opt)
  -- vim.api.nvim_set_keymap("n", "<leader>4", "$", opt)
  -- vim.api.nvim_set_keymap("n", "<leader>6", "^", opt)
  -- vim.api.nvim_set_keymap("n", "dw", "diw", { noremap = true, silent = true })
  -- vim.api.nvim_set_keymap("n", "cw", "ciw", { noremap = true, silent = true })
  -- vim.api.nvim_set_keymap("n", "gt", ":bnext<CR>", opt)
  -- vim.api.nvim_set_keymap("n", "gT", ":bprevious<CR>", opt)
  -- vim.api.nvim_set_keymap("n", "<leader>bn", ":bnext<CR>", { desc = "Next Buffer", noremap = true, silent = true })
  -- vim.api.nvim_set_keymap("n", "<leader>bf", ":bprevious<CR>", { desc = "Prev Buffer", noremap = true, silent = true })
  -- vim.api.nvim_set_keymap("n", "<A-j>", "<cmd>m .+1<cr>==", { desc = "Move down", noremap = true, silent = true })
  -- vim.api.nvim_set_keymap("n", "<A-k>", "<cmd>m .-2<cr>==", { desc = "Move up", noremap = true, silent = true })
  -- vim.api.nvim_set_keymap(
  --   "i",
  --   "<A-j>",
  --   "<esc><cmd>m .+1<cr>==gi",
  --   { desc = "Move down", noremap = true, silent = true }
  -- )
  -- vim.api.nvim_set_keymap("i", "<A-k>", "<esc><cmd>m .-2<cr>==gi", { desc = "Move up", noremap = true, silent = true })
  -- vim.api.nvim_set_keymap("v", "<A-j>", ":m '>+1<cr>gv=gv", { desc = "Move down", noremap = true, silent = true })
  -- vim.api.nvim_set_keymap("v", "<A-k>", ":m '<-2<cr>gv=gv", { desc = "Move up", noremap = true, silent = true })
  -- maps.n["gt"] = {
  --   function() require("astronvim.utils.buffer").nav(-(vim.v.count > 0 and vim.v.count or 1)) end,
  --   desc = "Previous buffer",
  -- }
  -- maps.n["]b"] =
  --   { function() require("astronvim.utils.buffer").nav(vim.v.count > 0 and vim.v.count or 1) end, desc = "Next buffer" }
  --
  -- vim.keymap.set("n", "j", [[v:count ? 'j' : 'gj']], { noremap = true, expr = true })
  -- vim.keymap.set("n", "k", [[v:count ? 'k' : 'gk']], { noremap = true, expr = true })
  -- vim.keymap.set("n", "<leader>ue", "<cmd>MarkdownPreviewToggle<CR>", opt)
  if vim.g.neovide then
    require "user.utils.neovide" -- Put anything you want to happen only in Neovide here
  end
end
